#include "NFGameConfigInterface.h"

#include "NFComm/NFPluginModule/NFIKernelModule.h"
#include "NFComm/NFPluginModule/NFIClassModule.h"
#include "NFComm/NFPluginModule/NFIElementModule.h"
#include "NFComm/NFMessageDefine/NFProtocolDefine.hpp"

bool NFGameConfigInterfaceModule::Init()
{
    m_pKernelModule = pPluginManager->FindModule<NFIKernelModule>();
    m_pClassModule = pPluginManager->FindModule<NFIClassModule>();
    m_pElementModule = pPluginManager->FindModule<NFIElementModule>();
    return true;
}

bool NFGameConfigInterfaceModule::Execute()
{
    return true;
}

bool NFGameConfigInterfaceModule::AfterInit()
{
    NF_SHARE_PTR<NFIClass> xLogicClass = m_pClassModule->GetElement(NFrame::Game::ThisName());
    if (xLogicClass)
    {
        const std::vector<std::string>& strIdList = xLogicClass->GetIDList();
        for (int i = 0; i < (int)strIdList.size(); ++i)
        {
            const std::string& strId = strIdList[i];
            int gameId = (int)m_pElementModule->GetPropertyInt(strId, NFrame::Game::GameID());
            int maxPlayerCount = (int)m_pElementModule->GetPropertyInt(strId, NFrame::Game::MaxPlayerCount());
            int maxSitCount = (int)m_pElementModule->GetPropertyInt(strId, NFrame::Game::MaxSitCount());
            int maxLookenCount = (int)m_pElementModule->GetPropertyInt(strId, NFrame::Game::MaxLookonCount());
            if (maxPlayerCount != maxSitCount + maxLookenCount)
            {
                std::string errMsg = "Game Config Error, maxPlayerCount != maxSitCount + maxLookenCount, ConfigID:" + lexical_cast<std::string>(strId);
                std::cout << errMsg << std::endl;
                NFASSERT(0, errMsg, __FILE__, __FUNCTION__);
            }

            NF_SHARE_PTR<QIPaiGameConfigParameter> param = NF_SHARE_PTR<QIPaiGameConfigParameter>(new QIPaiGameConfigParameter());
            param->SetGameName(strId);
            param->SetGameId(gameId);
            param->SetMaxPlayerCount(maxPlayerCount);
            param->SetMaxSitCount(maxSitCount);
            param->SetMaxLookenCount(maxLookenCount);

            mapGameConfig.AddElement(gameId, param);
            mapGameNameToGameId.emplace(strId, gameId);
        }
    }
    return true;
}

bool NFGameConfigInterfaceModule::Shut()
{
    mapGameConfig.ClearAll();
    return true;
}

NF_SHARE_PTR<QIPaiGameConfigParameter> NFGameConfigInterfaceModule::GetGameConfigParameter(const std::string& gameName)
{
    if (mapGameNameToGameId.find(gameName) == mapGameNameToGameId.end())
    {
        return nullptr;
    }

    int gameId = mapGameNameToGameId[gameName];
    return GetGameConfigParameter(gameId);
}

NF_SHARE_PTR<QIPaiGameConfigParameter> NFGameConfigInterfaceModule::GetGameConfigParameter(int gameId)
{
    return mapGameConfig.GetElement(gameId);
}